package com.share.domain.award.service;

import com.share.domain.award.event.SendAwardMessageEvent;
import com.share.domain.award.model.aggregate.UserAwardRecordAggregate;
import com.share.domain.award.model.entity.DistributeAwardEntity;
import com.share.domain.award.model.entity.TaskEntity;
import com.share.domain.award.model.entity.UserAwardRecordEntity;
import com.share.domain.award.model.valobj.taskStateVo;
import com.share.domain.award.repository.IAwardRepository;
import com.share.domain.award.service.distribute.IDistributeAward;
import com.share.types.event.BaseEvent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
@Slf4j
public class AwardService implements IAwardService {
    private final IAwardRepository awardRepository;

    private final SendAwardMessageEvent sendAwardMessageEvent;

    private final Map<String,IDistributeAward> distributeAwardMap;

    public AwardService(IAwardRepository awardRepository, SendAwardMessageEvent sendAwardMessageEvent, Map<String, IDistributeAward> distributeAwardMap) {
        this.awardRepository = awardRepository;
        this.sendAwardMessageEvent = sendAwardMessageEvent;
        this.distributeAwardMap = distributeAwardMap;
    }


    @Override
    public void saveUserAwardRecord(UserAwardRecordEntity userAwardRecordEntity) {
        //构建消息实体对象
        SendAwardMessageEvent.SendAwardMessage sendAwardMessage = new SendAwardMessageEvent.SendAwardMessage();
        sendAwardMessage.setAwardId(userAwardRecordEntity.getAwardId());
        sendAwardMessage.setOrderId(userAwardRecordEntity.getOrderId());
        sendAwardMessage.setAwardTitle(userAwardRecordEntity.getAwardTitle());
        sendAwardMessage.setUserId(userAwardRecordEntity.getUserId());
        sendAwardMessage.setAwardConfig(userAwardRecordEntity.getAwardConfig());

        BaseEvent.EventMessage<SendAwardMessageEvent.SendAwardMessage> sendAwardMessageEventMessage = sendAwardMessageEvent.buildBaseMessage(sendAwardMessage);

        //构建任务对象
        TaskEntity taskEntity = new TaskEntity();
        taskEntity.setUserId(userAwardRecordEntity.getUserId());
        taskEntity.setMessage(sendAwardMessageEventMessage);
        taskEntity.setTopic(sendAwardMessageEvent.topic());
        taskEntity.setState(taskStateVo.create);
        taskEntity.setMessageId(sendAwardMessageEventMessage.getId());

        UserAwardRecordAggregate userAwardRecordAggregate = new UserAwardRecordAggregate();
        userAwardRecordAggregate.setTaskEntity(taskEntity);
        userAwardRecordAggregate.setUserAwardRecordEntity(userAwardRecordEntity);


        awardRepository.saveUserAwardRecord(userAwardRecordAggregate);

    }

    @Override
    public void distributeAward(DistributeAwardEntity distributeAwardEntity) {
        //奖品key
        //todo  error
        String awardKey = awardRepository.queryAwardKey(distributeAwardEntity.getAwardId());
        if(null == awardKey){
            log.error("分发奖品，奖品ID不存在,awardId:{}",distributeAwardEntity.getAwardId());
            return;
        }
        IDistributeAward distributeAward = distributeAwardMap.get(awardKey);
        if(null == distributeAward){
            log.error("分发奖品，奖品key对应服务不存在,awardKey:{}",awardKey);
            //todo 后续完善全部奖品后开启异常
//            throw  new RuntimeException("分发奖品,奖品"+awardKey+"对应服务不存在");
            return;
        }
        distributeAward.giveOutPrize(distributeAwardEntity);


    }
}
